package gemini.castor.ui.client.page.content.office.todolist.form;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.utils.HtmlUtils;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class TodoListFormView extends Composite implements
		TodoListFormPresenter.Display {

	@UiTemplate("TodoListFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, TodoListFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;

	public TodoListFormView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}

	@UiField
	FlexTable ftbTodoList;

	@UiField
	TextBox tbxAddNew;

	@UiField
	Button btnAddNew;

	@UiHandler("btnAddNew")
	void onClick(ClickEvent e) {
		ftbTodoList.setHTML(i, 1, tbxAddNew.getText());
		HTML htmRemove = new HTML("<div id=\"btnDelete\">"
				+ HtmlUtils.TAG_SPACE + "</div>");
		htmRemove.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				boolean response = Window.confirm(constants.htmAreYouSure());
				if (response) {
				}
			}
		});
		ftbTodoList.setWidget(i, 2, htmRemove);
		ftbTodoList.setHTML(i, 0, i+"");
		i++;
		tbxAddNew.setText("");
	}

	@Override
	public Widget asWidget() {
		return this;
	}
	
	private int i;
	
	@Override
	public void loadData() {
		i=1;
		// TODO: NHS-Init data for order table
		initTodoList();
		initTodoTable();
		ftbTodoList.removeAllRows();
		initOrderHeader();
		RowFormatter rowFmt = ftbTodoList.getRowFormatter();
		
		for (String it : orderList) {
			if (i % 2 == 0) {
				rowFmt.setStyleName(i, "rowEven");
			}
			HTML htmRemove = new HTML("<div id=\"btnDelete\">"
					+ HtmlUtils.TAG_SPACE + "</div>");
			htmRemove.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					boolean response = Window.confirm(constants.htmAreYouSure());
					if (response) {
					}
				}
			});
			ftbTodoList.setWidget(i, 2, htmRemove);
			ftbTodoList.setHTML(i, 1, it);
			ftbTodoList.setHTML(i, 0, i+"");
			i++;
		}

		// TODO: NHS-Init data for downline table
	}

	private void initTodoTable() {
		ColumnFormatter colFmt = ftbTodoList.getColumnFormatter();

		colFmt.setWidth(0, "5%");
		colFmt.setWidth(1, "90%");
		colFmt.setWidth(2, "10%");
	}

	private void initOrderHeader() {
		ftbTodoList.setHTML(0, 0, HtmlUtils.bold(constants.htmOrderNumber()));
		ftbTodoList.setHTML(0, 1, HtmlUtils.bold(constants.htmTodo()));
		ftbTodoList.setHTML(0, 2, "");
		ftbTodoList.getRowFormatter().setStyleName(0, "tableHeader");
		ftbTodoList.setStyleName("ftbSummary");
	}

	private List<String> orderList = new ArrayList<String>();

	private void initTodoList() {
		orderList.clear();
		orderList.add("Gap chu Nguyen Viet Luong");
		orderList.add("Giao hang co Xuan quan 1");
		orderList.add("Hoi thao kho hang 224 Pasteur");
		orderList.add("Gap khach hang Q3");
	}

}
